package com.xzc.java8.C_Collect;

import com.alibaba.fastjson.JSON;

import java.util.*;

import static java.util.stream.Collectors.*;

public class Partitioning {

    public static void main(String[] args) {
        List<Apple> list0 = new ArrayList<>();
        Apple a0 = new Apple("a0", "red", 150, 2011);
        Apple a1 = new Apple("a1", "red", 10, 2012);
        Apple a2 = new Apple("a2", "red", 110, 2011);
        Apple a3 = new Apple("a3", "yellow", 140, 2013);
        list0.add(a0);
        list0.add(a1);
        list0.add(a2);
        list0.add(a3);

//        Map<Boolean, List<Apple>> out0 = list0.stream().collect(partitioningBy(Apple::isRedApple));
//        System.out.println(JSON.toJSONString(out0, true));
//
//        Map<Boolean, Map<Integer, List<Apple>>> out1 = list0.stream().collect(partitioningBy(Apple::isRedApple,
//                groupingBy(Apple::getYear)));
//        System.out.println(JSON.toJSONString(out1, true));
//
//        Map<Boolean, Apple> out2 = list0.stream().collect(partitioningBy(Apple::isRedApple,
//                collectingAndThen(maxBy(Comparator.comparingInt(Apple::getYear)), Optional::get)));
//        System.out.println(JSON.toJSONString(out2, true));

//        Map<Boolean, Map<Boolean, List<Apple>>> out3 = list0.stream().collect(partitioningBy(Apple::isRedApple,
//                partitioningBy(Apple::is2011)));
//        System.out.println(JSON.toJSONString(out3, true));

//        Map<Boolean, Map<Boolean, List<Apple>>> out4 = list0.stream().collect(partitioningBy(Apple::isRedApple,
//                partitioningBy(d -> d.getWeight() > 50)));
//        System.out.println(JSON.toJSONString(out4, true));

        Map<Boolean, Long> out5 = list0.stream().collect(partitioningBy(Apple::isRedApple,
                counting()));
        System.out.println(JSON.toJSONString(out5, true));
    }
}
